package com.wei.controller.web;

import com.wei.blog.common.exception.BlogException;
import com.wei.blog.common.help.UserHelp;

import com.wei.entity.UserEntity;
import com.wei.service.IFollowerService;
import com.wei.service.ILikeService;
import com.wei.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

@Controller
@RequestMapping("/account")
public class UserController {
    @Autowired
    private UserHelp userHelp;
    @Autowired
    private IFollowerService followerService;
    @Autowired
    private ILikeService likeService;
    @Autowired
    private UserService userService;


    @GetMapping("/info/{userId}")
    public String info(@PathVariable Integer userId, ModelMap map) {
        if (ObjectUtils.isEmpty(userId)) {
            throw new BlogException("用户未登录");
        }
        //从数据库查询用户信息
        UserEntity userEntity = userService.getById(userId);
        userEntity.setPwd(null);
        //查询用户点赞数量
        Long userLikeCount = likeService.getUserLikeCount(userId);
        //查询当前用户是否关注了该用户
        Boolean hasFollowed = false;
        if (userHelp.get() != null) {
            hasFollowed = followerService.findUserFollowerStatus(userId, userHelp.get().getUid());
        }
        //查询当前用户的关注总数
        Long followerCount = followerService.findUserFollowerCount(userId);
        //查询粉丝总数
        Long fansCount = followerService.findUserFansCount(userId);
        map.put("user", userEntity);
        map.put("userLikeCount", userLikeCount);
        map.put("hasFollowed", hasFollowed);
        map.put("followerCount", followerCount);
        map.put("fansCount", fansCount);
        map.put("loginUser", userHelp.get());
        return "site/profile";
    }



}
